package main

import "fmt"

func main() {
	a := []int{1, 2, 3, 4, 5, 6}
	s := a[:4]
	fmt.Println(s)
	fmt.Println(appendInt(s, 7))
	fmt.Println(s)
	fmt.Println(a)
}

func appendInt(s []int, x int) []int {
	var z []int
	zlen := len(s) + 1
	if zlen < cap(s) {
		z = s[:zlen]
	} else {
		//扩容
		zcap := zlen
		if zcap < 2*len(s) {
			zcap = 2 * len(s)
		}
		z := make([]int, zlen, zcap)
		copy(z, s)
	}
	z[len(s)] = x
	return z
}
